﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace DemoGeometry
{
    public class Connecttion
    {
        private SqlConnection connect;
        public SqlCommand command;
        public SqlDataAdapter adapter;
        private string constring="";
        public SqlDataReader readdata;
        SqlCommandBuilder commandBuilder ;
        private DataTable data;
        private  DataSet ds;
        public  Connecttion(string ls_connect)
        {
          //  constring = ConfigurationManager.ConnectionStrings[ls_connect].ConnectionString;
            constring=ConfigurationManager.ConnectionStrings[ls_connect].ConnectionString;
            connect = new SqlConnection(constring);
        }
        public void open()
        {
            if(connect.State== ConnectionState.Closed)
            {
                try
                {
                    connect.Open();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("提示：数据库连接出错" + ex.Message);
                }
              
            }

        }

     
        public void close()
        {
            connect.Close();
        }
        public void getcommand(string ls_sql)
        {
            if (connect == null)
            {
                connect.Open();
            }
            command = new SqlCommand(ls_sql, connect);

        }
        public void  read()
        {
            readdata = command.ExecuteReader(CommandBehavior.CloseConnection);

        }
        public void update()
        {
            command.ExecuteNonQuery();
        }
        public DataTable readdatatable()
        {
            data = new DataTable();
            adapter = new SqlDataAdapter(command);
            adapter.Fill(data);
            return data;
        }
        public DataSet Readdataset()
        {
            ds = new DataSet();
            adapter = new SqlDataAdapter(command);
            commandBuilder = new SqlCommandBuilder(adapter);
            adapter.Fill(ds);
            return ds;

        }
        public object ExecuteScalar()
        {
            return command.ExecuteScalar();
        }
        public void FillDataGrid(System.Windows.Forms.DataGridView gv,string ls_sql)
        {
            ds =new DataSet();
            adapter = new SqlDataAdapter(ls_sql,connect);
            adapter.Fill(ds);
            gv.DataSource = ds.Tables[0];   
        }

    }
}